package time;

import java.io.Serializable;

import javax.management.RuntimeErrorException;
import javax.naming.OperationNotSupportedException;

public class LogicalTimeStamp  implements Serializable{
	
	private int lTimeStamp=0;
	private int pos=-1; // the process id
	
	public LogicalTimeStamp(int position){
		lTimeStamp = 0;
		pos = position;
	}
	
	public boolean isEqual(LogicalTimeStamp a){
		if(this.lTimeStamp != a.lTimeStamp){
				return false;
		}
		return true;
	}
	
	protected void increment(LogicalTimeStamp a){
		if(a instanceof LogicalTimeStamp){
			LogicalTimeStamp old = (LogicalTimeStamp) a;
			if(old.lTimeStamp >= this.lTimeStamp){
				lTimeStamp = old.lTimeStamp + 1;
			}else{
				this.increment();
			}
		}
	}

	protected void increment() {
		lTimeStamp++;
	}
	
	public String toString(){
		return ""+this.lTimeStamp;
	}
	
	public int getLogicalTimeStamp(){
		return lTimeStamp;
	}

	protected void initialize(int pos2) {
		this.pos = pos2;
	}

	protected LogicalTimeStamp clone() {
		LogicalTimeStamp newTS = new LogicalTimeStamp(this.pos);
		newTS.lTimeStamp = this.lTimeStamp;
		return newTS;
	}
	
	public int getPos(){
		return this.pos;
	}

	public boolean happenedBefore(LogicalTimeStamp a) {
		if(this.lTimeStamp < a.lTimeStamp){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean equals(LogicalTimeStamp a){
		return isEqual(a);
	}
}
